package com.tuanzi.loan.web.bean.system.workflow;

import javax.faces.bean.ViewScoped;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.web.configuration.JSFBean;
import com.tuanzi.loan.workflow.entity.StartPage;
import com.tuanzi.loan.workflow.service.StartPageService;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * @author Ray 2017/09/22
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class WorkflowStartPageBean extends JSFBean {

	private static final long serialVersionUID = -2152778554108785342L;

	@Autowired
	private StartPageService startPageService;

	private String procDefId;
	private StartPage startPage;
	private String startPageUrl;

	public void onSave() {
		if (StringUtils.isBlank(startPageUrl)) {
			startPageService.deleteByProcDefId(procDefId);
		} else {
			if (startPage == null) {
				startPage = new StartPage();
				startPage.setUrl(startPageUrl);
				startPage.setProcDefId(procDefId);
			}
			startPageService.saveOrUpdate(startPage);
		}
		addSuccessfullyMessage();
	}

	public void onLoadStartPageData() {
		startPage = startPageService.findByProcDefId(procDefId);
		if (startPage != null) {
			startPageUrl = startPage.getUrl();
		}
	}

}
